var show_pay=1;
var questionPay = Number(getApp().globalData.askMoney);

var datas=[];
var wxNum;
var send=0;
var information=0;
var sendData;
var niming=true;
var kind;
var ihave=0;
var b=[];
var userInfo;
var pathimg=[];
var qiniuUploader = require("../qiniuUploader.js");
var time=require('../../utils/util.js');
var uploadimage = function (res) {

  for (var i = 0; i < res.length; i++) {

    qiniuUploader.upload(res[i], (res) => {
      console.log("上传成功")
    },
      (error) => {
        console.log("上传失败")
      },
      {
        uploadURL: 'https://up.qbox.me',
        region: 'ECN',

        domain: 'www.liuxuanlong.club',
        uptokenURL: 'https://www.liuxuanlong.xyz/fenda/wx/upToken'
      }
    );

  }
}
var uploadvoice = function (res) {
  if(res!=null){
    qiniuUploader.upload(res, (res) => {
      console.log("上传成功")
    },
      (error) => {
        console.log("上传失败")
      },
      {
        uploadURL: 'https://up.qbox.me',
        region: 'ECN',

        domain: 'oxk5a6sj2.bkt.clouddn.com',
        uptokenURL: 'https://www.lixiaole.xyz/fenda/wx/upToken'
      }
    );
  }

}
Page({
  data:{
    
    fail:"none",
    warn:true,
    iconX:"none",
    usersImg:"",
    imgs1:"none",
    imgNum:0,
    or:"",
    deg:"0deg",
    display:"none",
    display2:"none",
    modal:true,
    toast:true,
    value:"",
    usersImg:"",
    imgSwitch:"display",
    fontNum:0,
    loading:"display",
    font:"none"
  
    
  },
  pay: function () {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.liuxuanlong.xyz/fenda//wx/pay',
            data: {
              code: res.code,
              total_fee: questionPay
            },
            success: function (res) {
              show_pay = 1;
              console.log(res.data, '订单号');
              var out_trade_no = res.data.out_trade_no;
              wx.requestPayment(
                {
                  'appid': res.data.appid,
                  'timeStamp': res.data.timeStamp,
                  'nonceStr': res.data.nonceStr,
                  'package': res.data.package,
                  'signType': 'MD5',
                  'paySign': res.data.paySign,
                  'success': function (res) {
                    
                    that.submits(out_trade_no);
                  },
                  'fail': function (res) {
                    console.log('打印', res);
                  },
                  'complete': function (res) {
                    console.log("666")
                  }
                })
              /*wx.setStorage({
                key: "login_uuId",
                data: res.data
              });*/


              console.log('登录完毕')
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.avatarUrl)
        }
      }
    })
  },
  changeNum:function(e){
    var length=e.detail.value.length;
   this.setData(
     {
       fontNum:length
     }
   );
  },
  niming:function(e){
    niming=e.detail.value;
    
    console.log(niming);
  },
  userInfoHandler:function(){
   var that=this;
    wx.getUserInfo({
      success:function(res){
        console.log('获取成功!');
        userInfo=res.userInfo;
        ihave=1;
        if (datas.text == "") {
          that.setData(
            {
              warn: false
            }
          );
        }
        else {
          if(show_pay==1){
          wx.showToast({
            title: '加载中...',
            icon:'loading',
            duration:500
          })
          show_pay = 0;
          that.pay();
          
          }
         /* that.setData(
            {
              modal: false
            }
          );*/
          
        }
      

      },
    })
  },
  tosend: function (e) {              //form页面赋值
  console.log(ihave);
    
    var that=this;
    datas=e.detail.value;                      
   
   
    
  },
  toasthidden:function(){         
    this.setData(
      {
        toast:true
      }
    );
  },
  nowarn:function(){
    this.setData(
      {
        warn:true
      }
    );
  },
 
  ImgAndVoice:function(){             //提交语音与图片
    uploadimage(pathimg);
   // uploadvoice(pathvoice);
    pathimg=[];
    //pathvoice="";
  },
  submits: function (out_trade_no){                    //真提交
    var times =time.formatTime(new Date());
    var Time=Date.now();
    if(niming==true)
    niming='true';
    else
    niming='false';
    var that=this;     
    wx.getStorage({
      key: 'login_uuId',
      success: function(res) {
        wxNum=res.data;
        console.log(datas.text,'666')
        console.log('图片',pathimg);
        wx.request({
          url: "https://www.liuxuanlong.xyz/fenda/wx/addQuestion",
          data: {
            wxNumber: wxNum,
            info: datas.text,
            imgPath: pathimg,
            time: times,
            timeNum: Time,
            kind: kind,
            isOneMaster: 0,
            headImg: userInfo.avatarUrl,
            nickName: userInfo.nickName,
            isAnonymous: niming,
            total_fee: questionPay,
            out_trade_no: out_trade_no
            


          },
          method: 'GET',
          success: function (res) {
            console.log('发送成功')
            that.ImgAndVoice();

            datas = [];
            that.setData(
              {
                modal: true,
                toast: false,
                or: "提交成功",
                imgs1: "none",
                value: ""
              }
            );

            that.goto(res.data);


          },
          fail: function () {
            that.setData(
              {
                modal: true,
                fail: "display"
              }
            );
          },
        })
      },
    })
    
   
    
  },
  goto:function(data){
    console.log('返回uuid',data);
    wx.redirectTo({
      url: '../mine/myquestion/myquestion?uuid=' + data,
    })
  },
  nowarns: function () {
    this.setData(
      {
        fail: "none"
      }
    );
  },
  nosubmits:function(){               //提问之后取消按钮
    this.setData(
      {
        modal:true
      }
    );
  },
 
  showimgs:function(imgs){                      //用户上传之前预览点击
    
    if(pathimg.length>=3)
    this.setData(
      {
        imgSwitch:"none"
      }
    );
    else{
      this.setData(
        {
          imgSwitch:"display"
        }
      );
    }
    var a={"src":"","show":""};
    
  
    for(var i=0;i<imgs.length;i++){
     
      a.src=imgs[i];
      
      a.show='display';
      b.push(a);
      a = { "src": "", "show": "" };
     
    }
    console.log(b);
    this.setData(
      {
        /*usersImg:pathimg[0],
        imgs1:"display",
        imgNum:length,
        iconX:"display"*/
        usersImg:b
      }
    );
  },
  previewImg:function(e){                      //用户预览图片
   
    var index=e.currentTarget.id;
    var prewImg=[];
    prewImg.push(pathimg[index]);
    for(var i=0;i<pathimg.length;i++){
      if(i!=index)
      prewImg.push(pathimg[i]);
    }
      wx.previewImage({
        current: '',
        urls: prewImg,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
  },
  cancelImg:function(e){                       //用户取消图片
        
        var index=e.currentTarget.id;
        pathimg.splice(index,1);
        b.splice(index,1);
        this.setData(
          {
            usersImg: b,
            /*imgs1: "none",
            imgNum: 0,
            iconX:"none"*/
          }
        );
        if(pathimg.length<3){
          this.setData(
            {
              imgSwitch:"display"
            }
          );
        }
  },
  uploadp:function(){                            //选择图片
    var that=this;
    var chooseMax=3-b.length;
      wx.chooseImage({
        count: chooseMax, // 最多可以选择的图片张数，默认9
        sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
        sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
        success: function(res){
          // success
          pathimg=pathimg.concat(res.tempFilePaths);
          that.showimgs(res.tempFilePaths);
        },
        fail: function() {
          // fail
        },
        complete: function() {
          // complete
        }
      })
  },
 
  
  down:function(){                //责任声明下拉显示
      if(this.data.deg=='0deg')
      this.setData(
          {
              deg:"180deg",
              display:"block"
          }
      );
      else{
          this.setData(
              {
                  deg:"0deg",
                  display:"none"
              }
          );
      }
  },
  onLoad:function(options){
    // 生命周期函数--监听页面加载
    questionPay = getApp().globalData.askMoney;
    var that=this;
      kind=options.kind;
    this.setData(
      {
        questionPay: Number(questionPay)*0.01,
        
      }
    );
    setTimeout(function(){
      that.setData(
        {
          loading: "none",
          font: "display"
        }
      );
    },200);
      wx.setNavigationBarTitle({
        title: kind,
      })
      b=[];
      pathimg=[];
      
  },
  onReady:function(){
    
  },
  onShow:function(){
   
  },

})